package com.smyh.adapter;

import android.graphics.Color;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.smyh.UIM.MainActivity;
import com.smyh.UIM.R;
import com.smyh.UIM.TerminalParam;
import com.smyh.bean.RFDeviceInfo;
import com.smyh.cache.FreshCurrentView;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.common.MinaClientThread;
import com.smyh.protcol.CMDUtil;
import com.smyh.server.ToastShowMsgThread;
import com.smyh.util.HexStrUtil;

import org.apache.mina.core.buffer.IoBuffer;

import java.util.List;

/**
 * Created by 姜龙 on 2016/6/28.
 */
public class AddrItemAdapter extends BaseAdapter {
    float startX = 0;
    List<RFDeviceInfo> listRF = MainCahce.getDeviceInfos();
    public AddrItemAdapter() {
        super();
    }

    @Override
    public int getCount() {
        return MainCahce.getDeviceInfos().size();
    }



    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final RFDeviceInfo item = MainCahce.getDeviceInfos().get(position);
        View view;
        final ViewHolder viewHolder;
        if(convertView == null){
            view = LayoutInflater.from(MainActivity.context).inflate(R.layout.addr_item,null);
            viewHolder = new ViewHolder();
            viewHolder.device_addr  = (TextView) view.findViewById(R.id.device_addr);
            viewHolder.device_type  = (TextView) view.findViewById(R.id.device_type);
            viewHolder.is_online_img  = (ImageView) view.findViewById(R.id.is_online_img);
            viewHolder.addr_item  = (LinearLayout) view.findViewById(R.id.addr_item);
//            viewHolder.light_off  = (Button) view.findViewById(R.id.light_off);

            view.setTag(viewHolder);
        }else{
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        if(item.isSelect()){
            view.setBackgroundColor(Color.GRAY);
        }else{
            view.setBackgroundColor(Color.alpha(0xE0e0e0));
        }

        viewHolder.device_addr.setText(item.getAddr());
        viewHolder.device_type.setText(item.getDeviceType()+"");
        if(item.getStatus() == 1){
            viewHolder.is_online_img.setImageResource(R.drawable.green);
        }else{
            viewHolder.is_online_img.setImageResource(R.drawable.red);
        }
//        viewHolder.light_off.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                //灭灯
//                if( MinaClientThread.session!=null && MinaClientThread.session.isConnected()){
//                    byte[] deviceAddrBytes = HexStrUtil.hexStr2Bytes(item.getAddr());
//                    IoBuffer buffer = CMDUtil.on_offLight(deviceAddrBytes,false);
//                    MinaClientThread.session.write(buffer).awaitUninterruptibly();
//
//                    new Thread(new ToastShowMsgThread("灭灯指令下发")).start();
//                }
//            }
//        });
//        viewHolder.is_online_img.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if( MinaClientThread.session!=null && MinaClientThread.session.isConnected()){
//                    byte[] deviceAddrBytes = HexStrUtil.hexStr2Bytes(item.getAddr());
//                    IoBuffer buffer = CMDUtil.on_offLight(deviceAddrBytes,true);
//                    MinaClientThread.session.write(buffer).awaitUninterruptibly();
//
//                    new Thread(new ToastShowMsgThread("亮灯指令下发")).start();
//                }
//            }
//        });
        viewHolder.device_addr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainCahce.chooseDeviceAddr = item.getAddr();
                for (String key : MainCahce.deviceInfos.keySet()) {
                    if( MainCahce.chooseDeviceAddr.equals(key)){
                        MainActivity.home_resource.setText("RF地址: "+ MainCahce.chooseDeviceAddr);
                        MainCahce.deviceInfos.get( MainCahce.chooseDeviceAddr).setSelect(true);
                        //刷新主内容区内容：
                        FreshCurrentView.freshNowView();
                        continue;
                    }
                    MainCahce.deviceInfos.get(key).setSelect(false);
                }
                MainActivity.refreshDeviceList();
            }
        });
        viewHolder.device_addr.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                TerminalParam.actionStart(MainActivity.context,item.getAddr());
                return true;
            }
        });
        return view;
    }

    public  static class ViewHolder{
        public TextView device_addr;
        TextView device_type;
        ImageView is_online_img;
        LinearLayout addr_item;
//        Button light_off;
    }

}
